import Missing
from plone.indexer.decorator import indexer
from interfaces import IShortUrl, IShortUrlAdapter
from zope.component import queryAdapter

@indexer(IShortUrl)
def getContentShortCode(context, **kw):
    """
    This indexes IShortUrl objects' short url code using a named adapter.
    """
    if IShortUrl.providedBy(context):
        adapter = queryAdapter(context, IShortUrlAdapter, name="short-url-adapter")
        if not adapter:
            return Missing.Value
        else:
            return adapter.shortCode
    else:
        return Missing.Value

@indexer(IShortUrl)
def getContentRemoteCode(context, **kw):
    """
    This indexes IShortUrl objects' remote url code using a named adapter.
    """
    if IShortUrl.providedBy(context):
        adapter = queryAdapter(context, IShortUrlAdapter, name="short-url-adapter")
        if not adapter:
            return Missing.Value
        else:
            return adapter.remoteUrl
    else:
        return Missing.Value

